package com.yuncheng.oaApi.busService;

import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.oaApi.constant.OaCommonFlowConstant;
import com.yuncheng.entity.YcSysUser;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.oaApi.mapper.OaYcSysUserMapper;
import com.yuncheng.oaApi.service.IOaSysUserFgldService;
import com.yuncheng.oaApi.service.impl.OaYcSysUserServiceImpl;
import com.yuncheng.utils.ThreadContextHelper;
import com.yuncheng.oaApi.vo.AgentVo;
import com.yuncheng.vo.CurrentUserVo;
import com.yuncheng.vo.GetKsKz;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

@Component(value = "oadbrService")
public class OaDbrService {
    @Resource
    @Lazy
    private IOaSysUserFgldService sysUserFgldService;

    @Resource
    @Lazy
    private OaYcSysUserServiceImpl ycSysUserService;

    @Resource
    @Lazy
    private OaYcSysUserMapper oaYcSysUserMapper;



    // 根据待办人id，获取名称拼接起来
    public String getDbrsByIds(String dbrIds){
        //把待办人按id查出来  把名字拼起来
        String dbr = "";
        List<String> dbrIdsList = Arrays.asList(dbrIds.split(","));
        List<YcSysUser> sysUserList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(dbrIdsList)){
            sysUserList = ycSysUserService.listByIds(dbrIdsList);
        }

        if (CollectionUtils.isNotEmpty(sysUserList)){
            for (YcSysUser user : sysUserList){
                dbr += user.getRealname() + ",";
            }
        }

        return dbr;
    }

    // 办公室离岗负责人
    public Map getBgsLgFzrByDeptName(){
        Map map = new HashMap();
        String dbrId = "";
        String dbr = "";
        List<GetKsKz> ksKzObject = new ArrayList<>();
        ksKzObject = ycSysUserService.getKsKzObject(OaCommonFlowConstant.BGS_SP_FLOW, OaCommonFlowConstant.ROLE_NAME_BGSLGFZR);
        if (CollectionUtils.isNotEmpty(ksKzObject)) {
            for (GetKsKz ksKz : ksKzObject) {
                dbrId += ksKz.getId()+",";
                dbr += ksKz.getRealname()+",";
            }
        }else {
            throw new BusinessException("办公室离岗负责人不存在，请联系管理员");
        }

        map.put("dbrId",dbrId);
        map.put("dbr",dbr);
        return map;
    }


    // 对应科室的 科室负责人
    public Map getKsfzrByDeptName(String deptName){
        Map map = new HashMap();
        String dbrId = "";
        String dbr = "";
        List<GetKsKz> ksKzObject = new ArrayList<>();
        ksKzObject = ycSysUserService.getKsKzObject(deptName, OaCommonFlowConstant.ROLE_NAME_KZ);
        if (CollectionUtils.isNotEmpty(ksKzObject)) {
            for (GetKsKz ksKz : ksKzObject) {
                dbrId += ksKz.getId() + ",";
                dbr += ksKz.getRealname() + ",";
            }
        }

        map.put("dbrId", dbrId);
        map.put("dbr", dbr);
        return map;
    }


    // 获取对应科室的 分管领导
    public Map getKsFgldByDeptName(String deptName) {
        Map map = new HashMap();
        String dbrId = "";
        String dbr = "";

        YcSysUser fgldUserBydept = sysUserFgldService.getFgldUserBydept(deptName);
        if (fgldUserBydept != null) {
            dbrId = fgldUserBydept.getId();
            dbr = fgldUserBydept.getRealname();
        }

        map.put("dbrId",dbrId);
        map.put("dbr",dbr);
        return map;
    }

    //中心领导
    public Map getZxld(){
        Map map = new HashMap();
        String dbrId = "";
        String dbr = "";
        // 中心主任
        List<YcSysUser> zxzrList = ycSysUserService.list(new LambdaQueryWrapper<YcSysUser>()
                .eq(YcSysUser::getOrgName, OaCommonFlowConstant.ORG_NAME_ZXZR));
        if (CollectionUtils.isNotEmpty(zxzrList)){
            dbrId = zxzrList.get(0).getId();
            dbr = zxzrList.get(0).getRealname();
        }

        map.put("dbrId",dbrId);
        map.put("dbr",dbr);
        return map;
    }




    //中心副主任
    public Map getZxfld(){
        Map map = new HashMap();
        String dbrId = "";
        String dbr = "";
        // 中心主任
        List<YcSysUser> zxzrList = ycSysUserService.list(new LambdaQueryWrapper<YcSysUser>()
                .eq(YcSysUser::getOrgName, OaCommonFlowConstant.ORG_NAME_ZXFZR));
        if (CollectionUtils.isNotEmpty(zxzrList)){
            dbrId = zxzrList.get(0).getId();
            dbr = zxzrList.get(0).getRealname();
        }

        map.put("dbrId",dbrId);
        map.put("dbr",dbr);
        return map;
    }

    /**
     * 将待办人map转为List
     * @param map 待办人map
     */
    public List<AgentVo> getDbrListByMap(Map map){
        String dbrids = map.get("dbrId").toString(); // 待办人id
        String dbrs = map.get("dbr").toString(); // 待办人

        List<AgentVo> agentList = new ArrayList<>();

        if (org.apache.commons.lang3.StringUtils.isNotBlank(dbrids)) {
            String agentIds[] = dbrids.split(",");
            String agentNames[] = dbrs.split(",");

            for (int i = 0; i < agentIds.length; i++) {
                AgentVo agentVo = new AgentVo();
                agentVo.setDbrid(agentIds[i]);
                agentVo.setDbr(agentNames[i]);
                agentList.add(agentVo);
            }
        }
        return agentList;
    }

    // 对应科室的 科室负责人的id
    public String getKsfzrByDeptNames(String deptName) {
        Map map = new HashMap();
        String dbrId = "";
        List<GetKsKz> ksKzObject = new ArrayList<>();
        ksKzObject = ycSysUserService.getKsKzObject(deptName, OaCommonFlowConstant.ROLE_NAME_KZ);
        if (CollectionUtils.isNotEmpty(ksKzObject)) {
            for (GetKsKz ksKz : ksKzObject) {
                dbrId += ksKz.getId() + ",";
            }
        }
        String dbrids = dbrId;
        if (StringUtils.isNotBlank(dbrids)) {
            return dbrId;
        }
        return ("没有找到待办人id");
    }

    // 对应科室的 科室负责人的id
    public Map getKsfzrByDeptid(String deptName) {
        Map map = new HashMap();
        String dbrId = "";
        String dbr = "";
        List<GetKsKz> ksKzObject = new ArrayList<>();
        ksKzObject = ycSysUserService.getKsKzObject(deptName, OaCommonFlowConstant.ROLE_NAME_KZ);
        if (CollectionUtils.isNotEmpty(ksKzObject)) {
            for (GetKsKz ksKz : ksKzObject) {
                dbrId += ksKz.getId() + ",";
            }
        }
        map.put("dbrId", dbrId);

        return map;
    }

    //判断当前用户是否是科室负责人
    public boolean brksfzr() {

        CurrentUserVo currentUser = ThreadContextHelper.getCurrentUser();
        String nowid = currentUser.getId();
        Map ksfzrByDeptName = getKsfzrByDeptid(currentUser.getOrgName());
        boolean containsFuzzyValue = ksfzrByDeptName.values().stream().anyMatch(value -> StringUtils.containsIgnoreCase((CharSequence) value, nowid));

        return containsFuzzyValue;

    }

    // 会议安排负责人
    public Map gethyapFzrByDeptName() {
        Map map = new HashMap();
        String dbrId = "";
        String dbr = "";
        List<GetKsKz> ksKzObject = new ArrayList<>();
        ksKzObject = ycSysUserService.getKsKzObject(OaCommonFlowConstant.BGS_SP_FLOW, OaCommonFlowConstant.ROLE_NAME_HYAPFZR);
        if (CollectionUtils.isNotEmpty(ksKzObject)) {
            for (GetKsKz ksKz : ksKzObject) {
                dbrId += ksKz.getId() + ",";
                dbr += ksKz.getRealname() + ",";
            }
        } else {
            throw new BusinessException("会议安排负责人不存在，请联系管理员");
        }

        map.put("dbrId", dbrId);
        map.put("dbr", dbr);
        return map;
    }

    // 判断当前科室的分管领导和中心领导是否是同一人
    public boolean fgldyzxld(String deptName) {
        String zxldid = "";
        List<YcSysUser> zxzrList = ycSysUserService.list(new LambdaQueryWrapper<YcSysUser>()
                .eq(YcSysUser::getOrgName, OaCommonFlowConstant.ORG_NAME_ZXZR)); // 中心主任
        for (YcSysUser zxld : zxzrList) {
            zxldid = zxld.getId();
        }
        String fgldId = "";
        YcSysUser fgldUserBydept = sysUserFgldService.getFgldUserBydept(deptName); //当前使用科室的部门分管领导
        if (fgldUserBydept != null) {
            fgldId = fgldUserBydept.getId();
        }

        boolean sftyir = zxldid.equals(fgldId);
        return sftyir;
    }


    public String getDbrsByids(String dbrIds) {
        //把待办人按id查出来  把名字拼起来
        String dbr = "";
        List<String> dbrIdsList = Arrays.asList(dbrIds.split(","));
        List<YcSysUser> sysUserList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(dbrIdsList)) {
            sysUserList = ycSysUserService.listByIds(dbrIdsList);
        }

        if (CollectionUtils.isNotEmpty(sysUserList)) {
            for (YcSysUser user : sysUserList) {
                dbr += user.getRealname() + ",";
            }
        }

        return dbr;
    }

    //办公室收发文负责人不存在
    public Map getBgssfrfzr(){
        Map map = new HashMap();
        String dbrId = "";
        String dbr = "";
        List<GetKsKz> ksKzObject = new ArrayList<>();
        ksKzObject = ycSysUserService.getKsKzObject(OaCommonFlowConstant.BGS_SP_FLOW, OaCommonFlowConstant.ROLE_NAME_SFWFZR);
        if (CollectionUtils.isNotEmpty(ksKzObject)) {
            for (GetKsKz ksKz : ksKzObject) {
                dbrId += ksKz.getId()+",";
                dbr += ksKz.getRealname()+",";
            }
        }else {
            throw new BusinessException("办公室收发文负责人不存在，请联系管理员");
        }

        map.put("dbrId",dbrId);
        map.put("dbr",dbr);
        return map;
    }


    // 对应部门的岗位的的办理人
    public Map getDeptName(String deptName,String gw){
        Map map = new HashMap();
        String dbrId = "";
        String dbr = "";
        List<GetKsKz> ksKzObject = new ArrayList<>();
        ksKzObject = ycSysUserService.getKsKzObject(deptName,gw);
        if (CollectionUtils.isNotEmpty(ksKzObject)) {
            for (GetKsKz ksKz : ksKzObject) {
                dbrId += ksKz.getId() + ",";
                dbr += ksKz.getRealname() + ",";
            }
        }
        map.put("dbrId", dbrId);
        map.put("dbr", dbr);
        return map;
    }

    //根据待办人id 拼接待办人姓名
    public String getDbrName(String ids){
        if (StringUtils.isBlank(ids)){
            throw  new BusinessException("用户id为空");
        }
        String name = "";
        List<String> strings = Arrays.asList(ids.split(",|，"));
        List<YcSysUser> ycSysUsers = ycSysUserService.listByIds(strings);
        if (ycSysUsers == null){
            throw  new BusinessException("用户不存在");
        }
        for (YcSysUser user: ycSysUsers){
            if (StringUtils.isBlank(name)){
                name += user.getRealname();
            }else {
                name += ","+ user.getRealname();
            }
        }
        return name;
    }

    //根据部门名称 拼接待办人姓名 和id
    public Map getDepartName(String depart){

        if (StringUtils.isBlank(depart)){
            throw  new BusinessException("部门不存在");
        }
        Map map = new HashMap();
        String name = "";
        String id = "";
        List<YcSysUser> userList = ycSysUserService.list(new LambdaQueryWrapper<YcSysUser>().eq(YcSysUser::getOrgName, depart));
        if (userList == null){
            throw  new BusinessException("部门不存在");
        }
        for (YcSysUser user:userList){
            name += user.getId() + ",";
            id += user.getRealname() + ",";
        }
        map.put("dbrId", id);
        map.put("dbr", name);
        return map;
    }

    //根据岗位id 拼接待办人id 和待办人名称
    public Map getGw(String gwid){
        if (StringUtils.isBlank(gwid)){
            throw  new BusinessException("岗位不存在");
        }
        Map map = new HashMap();
        String name = "";
        String id = "";
        List<YcSysUser> gwDbr = oaYcSysUserMapper.getGwDbr(gwid);
        if (gwDbr == null){
            throw  new BusinessException("岗位不存在");
        }
        for (YcSysUser gw:gwDbr){
            name += gw.getId() + ",";
            id += gw.getRealname() + ",";
        }
        map.put("dbrId", id);
        map.put("dbr", name);
        return map;
    }




}
